package com.migu.contacts.main;

import static com.migu.contacts.net.Service.API;

import android.util.Log;
import android.view.View;

import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.BaseAdapter;
import com.frank.libandroid.CenterPopup;
import com.frank.libandroid.DataStore;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.Key;
import com.frank.libandroid.butterknife.BindAdapter;
import com.frank.libandroid.butterknife.OnClick;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.R;
import com.migu.contacts.bean.FestivalBean;
import com.migu.contacts.bean.GiftBean;
import com.migu.contacts.bean.GroupBean;
import com.migu.contacts.databinding.ActivityGiftSelectBinding;
import com.migu.contacts.databinding.ItemGiftBinding;
import com.migu.contacts.databinding.ItemGiftFitlerBinding;
import com.migu.contacts.databinding.ItemGitSelectPopFestivalBinding;
import com.migu.contacts.databinding.PopGiftFilterBinding;
import com.migu.contacts.util.GiftUtil;

import java.util.List;

public class GiftSelectActivity extends BaseActivity<ActivityGiftSelectBinding> {
    public static final Key<GiftBean> KEY = new Key<GiftBean>() {
    };
    @BindAdapter(R.id.recyclerView)
    BaseAdapter<ItemGiftBinding, GiftBean> baseAdapter = new BaseAdapter<ItemGiftBinding, GiftBean>() {
        @OnClick(R.id.confirm)
        public void confirm(GiftBean bean) {
            DataStore.put(KEY, bean);
            setResult(RESULT_OK);
            finish();
        }

        @Override
        protected void onBindData(ItemGiftBinding binding, GiftBean bean, int position) {
            super.onBindData(binding, bean, position);
            binding.confirm.setBackgroundResource(GiftUtil.getColor());
        }
    };

    @Override
    protected void initData() {
        super.initData();
        getData();
    }

    public void getData() {
        API.getGift(selectedGroup == null ? null : selectedGroup.id, selectedSex == 0 ? null : selectedSex, selectedFestival == null ? null : selectedFestival.id).watch(this, new HttpCallback<List<GiftBean>>() {
            @Override
            protected void onSuccess(List<GiftBean> bean) {
                Log.d(TAG, "onSuccess() called with: bean = [" + bean + "]");
                baseAdapter.setData(bean);
            }
        });
    }

    FestivalBean selectedFestival = null;
    GroupBean selectedGroup = null;
    int selectedSex = 0;

    public void showFilter(View view) {
        CenterPopup bottomPopup = new CenterPopup<PopGiftFilterBinding>(mActivity) {
            FestivalBean checkedFestival = null;
            GroupBean checkedGroup = null;
            int sex = 0;

            @OnClick(R.id.boyClick)
            public void boyClick() {
                if (mBinding.boyClick.isSelected()) {
                    return;
                }
                mBinding.girlClick.setSelected(false);
                mBinding.boyClick.setSelected(true);
                sex = 1;
            }

            @OnClick(R.id.girlClick)
            public void girlClick() {
                if (mBinding.girlClick.isSelected()) {
                    return;
                }
                mBinding.boyClick.setSelected(false);
                mBinding.girlClick.setSelected(true);
                sex = 2;
            }

            @BindAdapter(R.id.recyclerView1)
            BaseAdapter<ItemGiftFitlerBinding, GroupBean> adapter1 = new BaseAdapter<ItemGiftFitlerBinding, GroupBean>() {
                @Override
                protected void onBindData(ItemGiftFitlerBinding binding, GroupBean bean, int position) {
                    super.onBindData(binding, bean, position);
                    if (bean == checkedGroup) {
                        binding.confirm.setSelected(true);
                    } else {
                        binding.confirm.setSelected(false);
                    }
                    binding.confirm.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            checkedGroup = bean;
                            notifyDataSetChanged();
                        }
                    });
                }
            };
            @BindAdapter(R.id.recyclerView2)
            BaseAdapter<ItemGitSelectPopFestivalBinding, FestivalBean> adapter2 = new BaseAdapter<ItemGitSelectPopFestivalBinding, FestivalBean>() {

                @Override
                protected void onBindData(ItemGitSelectPopFestivalBinding binding, FestivalBean bean, int position) {
                    super.onBindData(binding, bean, position);
                    if (bean == checkedFestival) {
                        binding.confirm.setSelected(true);
                    } else {
                        binding.confirm.setSelected(false);
                    }
                    binding.confirm.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            checkedFestival = bean;
                            notifyDataSetChanged();
                        }
                    });
                }
            };

            @OnClick(R.id.confirm)
            public void confirm() {
                selectedFestival = checkedFestival;
                selectedGroup = checkedGroup;
                selectedSex = sex;
                dismiss();
                getData();
            }

            @Override
            protected void initData() {
                super.initData();
                API.queryFestival().watch(mActivity, new HttpCallback<List<FestivalBean>>() {
                    @Override
                    protected void onSuccess(List<FestivalBean> groupBeans) {
                        adapter2.setData(groupBeans);
                    }
                });
                API.queryGroup().watch(mActivity, new HttpCallback<List<GroupBean>>() {
                    @Override
                    protected void onSuccess(List<GroupBean> groupBeans) {
                        adapter1.setData(groupBeans);
                    }
                });
            }

            @OnClick(R.id.close)
            public void close() {
                dismiss();
            }
        };
        new XPopup.Builder(mActivity).
                asCustom(bottomPopup).
                show();
    }
}